<?php
/**
 *         
 * #######################################################################
 *           +----------------------------------------------+
 *           | THIS FILE IS A PART OF "PHP ZONES" FRAMEWORK | 
 *           +----------------------------------------------+
 *           
 *  	THIS CODE IS PROTECTED UNDER Apache Software License
 *  	http://www.apache.org/licenses/LICENSE-2.0
 *
 * 	Simply stating :
 * 		Proprietary Software linking: Yes
 * 		Distribution of this software : Yes
 * 		Redistributing of the code with changes: You can
 * 		Compatible with GNU GPL : NO :D
 *
 * 	Feel free to change and use but don't need to share the code.
 *
 * 	www.phpzones.com
 * #######################################################################
 */

	class Controller{
		
		private $theme; 
		private $locale;
		
		public $layout;
		public 	$layoutParams = array();
		
		private	$viewName;
		private $viewParams = array();
		
		public $session;
		public $post;
		public $get;
		public $cookie;
		public $showBenchmark = true;
		
		public function index(){
			// Hmm.. i.e. URL not found, redirect to home page? 
			// it may create a loop if the default zone does not override
			// the index method.
			zRedirect('index');
		}
		public function __construct(){

			$this->session = new ControllerSession();
			$this->post	= new ControllerPost();
			$this->get = new ControllerGet();
			$this->cookie = new ControllerCookie();
			
			$this->layout = 'none';
			
			// Initialize theme & locale from the session
			$this->theme = zCurrentTheme();
			$this->locale = zCurrentLocale();
		
		}
	
		// Set the theme and store it to the session
		public function updateTheme($theme){
			$this->theme = $theme;
			zCurrentTheme($theme);
		}
	
		// Set the locale and store it to the session
		public function updateLocale($locale){
			$this->locale = $locale;
			zCurrentLocale($locale);
		}
		
		public function show($view='',$params=array()){
			
			$this->viewName = $view;
			$this->viewParams = $params;
			
			// try to load the layout
			if($this->layout == 'none'){ // no layout to show
				if(empty($view)){
					zDie('Nothing to show! no view, no layout');
				}
				$this->insertView();
			}else{
				
				$layoutFile = zGetLayoutPath($this->layout);
				
				
				
				
				global $zCurrentView;
				global $zCurrentViewParams;
				global $zCurrentLayoutParams;
				
				$zCurrentLayoutParams =  $this->layoutParams;
				extract($zCurrentLayoutParams);
				$zCurrentViewParams = $params;
				$zCurrentView = $this->viewName;
				if(file_exists($layoutFile)){
					include $layoutFile;
				}
				else{
					echo 'PZERROR: no layout found>';
				}
			}
			
		}
		
		
		/**
		 * Should be only used inside a Layout.
		 * @return unknown_type
		 */
		private function insertView(){
			global $zZoneName;
			$viewFile = zGetViewPath($zZoneName,$this->viewName,'zones');
			if($viewFile == false)zDie("View file not found");
						
			extract($this->viewParams);
			
			include($viewFile);
		}
		
		public function addLayoutParam($key,$value){
			$this->layoutParams[$key] = $value;
		}
		
		public function showLayout($params = array()){
			$this->includeLayout($params);
		}
		
		public function skipCall(){
			return false;
		}
		
		public function setLayout($layout){
			//$this->layout = zCurrentLocale().'-'.$layout;
			$this->layout = $layout;
		}
	}
	
	
?>